//
//  AlbumBaseView.m
//  AlbumDemo
//
//  Created by nadyboy on 2018/1/29.
//  Copyright © 2018年 hejinhui. All rights reserved.
//

#import "AlbumBaseView.h"
#import "AlbumCell.h"
#import "AlbumCellModel.h"

#define margin 10.f
#define ScreenWidth      [UIScreen mainScreen].bounds.size.width
#define ScreenHeight     [UIScreen mainScreen].bounds.size.height

@interface AlbumBaseView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    CGFloat _cellWidth;
    CGFloat _cellHeight;
}

@property(nonatomic,strong)UICollectionView *collectionView;
@end
static NSString *cellId = @"cellid";
@implementation AlbumBaseView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
//        [self addSubview:self.collectionView];
    }
    return self;
}

-(void)setDataSource:(NSArray<AlbumCellModel *> *)dataSource
{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}
-(void)albumReload
{
    [self.collectionView reloadData];
}

-(instancetype)initWithAblum:(NSInteger)totalNumbers column:(NSInteger)column
{
    if (self = [super init]) {
      
        //宽度
        CGFloat width = (ScreenWidth - margin*(column+1))/column;
        //高度
        CGFloat height = 534*width/834+30;
        _cellWidth = width;
        _cellHeight = height;
        
        [self addSubview:self.collectionView];
    }
    return self;
}

/**
  init collectionview
 */
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:layOut];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[AlbumCell class] forCellWithReuseIdentifier:cellId];
    }
    return _collectionView;
}

#pragma mark -- collectionDataSource,delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    AlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    AlbumCellModel *model = _dataSource[indexPath.row];
    model.cellFrame = CGRectMake(0, 0, _cellWidth, _cellHeight);
    cell.cellModel = model;
    cell.backgroundColor = [UIColor redColor];
  
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(margin, margin, margin, margin);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(_cellWidth, _cellHeight);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    AlbumCellModel *model = _dataSource[indexPath.row];
    AlbumCell *cell = (AlbumCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    if (self.albumDelegate && [self.albumDelegate respondsToSelector:@selector(albumnDidSelected: albumModel:albumView:)]) {
        [self.albumDelegate albumnDidSelected:cell albumModel:model albumView:self];
    }
    NSLog(@"----%ld",indexPath.row);
}
@end
